package com.migo.thread;

import com.migo.entity.FeedbackEntity;
import com.migo.oss.OSSFactory;
import com.migo.service.FeedbackService;
import com.migo.utils.JsonUtils;

import java.util.Date;
import java.util.Map;

/**
 * Created by Wes on 2018/12/5.
 */
public class FeedbackThread implements Runnable {

    private FeedbackService feedbackService;
    private String params;
    private OSSFactory ossFactory;

    public FeedbackThread(String params, FeedbackService feedbackService, OSSFactory ossFactory) {
        this.params = params;
        this.feedbackService = feedbackService;
        this.ossFactory = ossFactory;
    }


    @Override
    public void run() {
        Map<String, String> mapFromJson = JsonUtils.getMapFromJson(this.params);
//            JSONArray array = JSONArray.fromObject(mapFromJson.get("images"));
//            StringBuffer imageBuffer = new StringBuffer();
//            int len = array.size();
//            for (int j = 0; j < len; j++) {
//                String imageType = ImageBase64Utils.getImageType(String.valueOf(array.get(j)));
//                String imageStr = ImageBase64Utils.replacePre(String.valueOf(array.get(j)));
//                System.out.print(imageType + "-----------" + imageStr);
//                BASE64Decoder decoder = new BASE64Decoder();
//                // Base64解码
//                byte[] b = decoder.decodeBuffer(imageStr);
//                for (int i = 0; i < b.length; ++i) {
//                    if (b[i] < 0) {// 调整异常数据
//                        b[i] += 256;
//                    }
//                }
//                // 生成jpeg图片
//                String imgFilePath = UUID.randomUUID().toString() + "." + imageType;// 新生成的图片
//                String url = this.ossFactory.build().upload(imgFilePath, b);
//                imageBuffer.append(url);
//                if (j < len - 1) {
//                    imageBuffer.append(",");
//                }
//                System.out.print("-----------" + url);
//            }
        String qt = mapFromJson.get("qstDescription");
        String userId = mapFromJson.get("userId");
        String productSelect = mapFromJson.get("productSelect");
        String moduleClassification = mapFromJson.get("moduleClassification");
        String suggestType = mapFromJson.get("suggestType");
        FeedbackEntity feedback = new FeedbackEntity();
        feedback.setUserId(userId);
        feedback.setAddTime(new Date());
        feedback.setImages("");
        feedback.setModuleclassification(moduleClassification);
        feedback.setSuggesttype(suggestType);
        feedback.setProductName(productSelect);
        feedback.setQtDescription(qt);
        this.feedbackService.save(feedback);
    }
}
